﻿using Appraisal_System.Utility;
using Appraisal_System_Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appraisal_System_Models
{
  public  class Users
    {
        public int Id { get; set; }
        public String UserName { get; set; }
        public String Sex { get; set; }
        public String PassWord { get; set; }
        public int BaseTypeId { get; set; }
        public bool IsDel { get; set; }

        public static    List<Users> ListAll()
        {
            DataTable dt = SqlHelper.ExecuteTable("SELECT u.Id,u.UserName,u.Sex,u.Password,u.BaseTypeId,u.IsDel from Users u");
            List<Users> users = new List<Users>();
            foreach(DataRow dr in dt.Rows)
            {
                users.Add(dr.DataRowToModel<Users>());
            }
            return users;
        }

        public static int Insert(Users user)
        {
            //防止注入攻击
            return SqlHelper.ExecuteNonQuery($"INSERT INTO Users(UserName,Sex,PassWord,BaseTypeId,IsDel) VALUES (@UserName,@Sex,@PassWord,@BaseTypeId,@IsDel)",
                new SqlParameter("@UserName", user.UserName),
                new SqlParameter("@Sex", user.Sex),
                new SqlParameter("@PassWord", user.PassWord), 
                new SqlParameter("@BaseTypeId", user.BaseTypeId),
                new SqlParameter("@IsDel", user.IsDel));
        }

        public static int Update(Users user)
        {
            return SqlHelper.ExecuteNonQuery($"UPDATE Users SET UserName=@UserName,Sex=@Sex,PassWord=@PassWord,BaseTypeId=@BaseTypeId,IsDel=@IsDel WHERE Id=@Id",
                new SqlParameter("@Id",user.Id),
                 new SqlParameter("@UserName", user.UserName),
                new SqlParameter("@Sex", user.Sex),
                new SqlParameter("@PassWord", user.PassWord),
                new SqlParameter("@BaseTypeId", user.BaseTypeId),
                new SqlParameter("@IsDel", user.IsDel));
        }
    }
}
